/*
 * 文 件 名:  HttpClient.java
 * 版    权:   深圳埃思欧纳信息咨询有限公司版权所有. YYYY-YYYY,  All rights reserved
 * 描    述:   <描述>
 * 修 改 人:  honny.huang
 * 修改时间: 2011-3-15
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.jsoup;


/**
 * <一句话功能简述> <功能详细描述>
 *
 * @author honny.huang
 * @version [版本号, 2011-3-15]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */

public class HttpClient 
{
	private String swfUrl;
	
	/**
	 * <解析视频地址页面>
	 * 
	 * @param sqlDataSource
	 *            参数集
	 * @return []
	 * 
	 * @return Map [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public  String parseUrl(String url) {   // http://v.youku.com/v_playlist/f5618298o1p0.html
		String resultStr = "";
		// 地址解析标志，默认未解析为noChg,解析后是chg
		// String parseFlag = "noChg";
		if (url.indexOf("v.youku.com") != -1) {
			resultStr = parseYouKuUrl(url);

		} else if (url.indexOf("www.56.com") != -1) {
			resultStr = parse56Url(url);
		} else {
			resultStr = "Warning:" + getOldUrl(url);
		}
		return resultStr;
	}
	
	/**
	 * <解析优酷视频地址页面>
	 * 
	 * @param sqlDataSource
	 *            参数集
	 * @return []
	 * 
	 * @return Map [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public  String parseYouKuUrl(String url) { // http://v.youku.com/v_playlist/f5618298o1p0.html
		System.out.print("开始解析youku视频");
		// Map resultMap = new HashMap();
		String resultStr = "";
		String contextStr = HttpClientUtil.getPostResponseWithHttpClient(url,
				"utf-8");

		StringBuffer sb = new StringBuffer("");
		if (contextStr != null && contextStr.length() > 0
				&& !"error".equals(contextStr)) {
			String titleStr = "";
			titleStr = contextStr.substring(contextStr.indexOf("<title>") + 7,
					contextStr.indexOf("</title>"));
			// resultMap.put("videoTitle", titleStr);
			String imgStr = "";
			imgStr = contextStr.substring(contextStr.indexOf("id=\"download\""), contextStr.length());
			imgStr = imgStr.substring(imgStr.indexOf("http") + 4);
			imgStr = imgStr.substring(imgStr.indexOf("http"), imgStr.indexOf("|\""));
//			imgStr = imgStr.substring(imgStr.indexOf("http"), imgStr.indexOf("|"));
			// resultMap.put("videoPhoto", imgStr);
			// System.out.print(imgStr);
			String playUrlStr = "";
			playUrlStr = contextStr.substring(contextStr
					.indexOf("id=\"link2\""), contextStr.length());  // http://player.youku.com/player.php/Type/Folder/Fid/5618298/Ob/1/Pt/0/sid/XMjUwODgzNDMy/v.swf
			playUrlStr = playUrlStr.substring(playUrlStr.indexOf("http"),
					playUrlStr.length());
			playUrlStr = playUrlStr.substring(0, playUrlStr.indexOf("\""));
			this.setSwfUrl(playUrlStr);
			// resultMap.put("videoUrl", playUrlStr);
			sb.append("<videoinfo TotalRecords='1'>");
			sb.append("<Row>");
			sb.append("<videoTitle>");// 视频标题
			sb.append("<![CDATA[" + titleStr + "]]>");
			sb.append("</videoTitle>");
			sb.append("<videoUrl>");// 视频url
			sb.append("<![CDATA[" + playUrlStr + "]]>");
			sb.append("</videoUrl>");
			sb.append("<videoPhoto>");// 视频封面
			sb.append("<![CDATA[" + imgStr + "]]>");
			sb.append("</videoPhoto>");
			// sb.append("<opFlag>");//操作标志
			// sb.append("<![CDATA[0]]>");
			// sb.append("</opFlag>");
			sb.append("</Row>");
			sb.append("</videoinfo>");
			resultStr = sb.toString();
		} else {
			resultStr = getOldUrl(url);
		}
		return resultStr;
	}

	/**
	 * <解析优酷视频地址页面>
	 * 
	 * @param sqlDataSource
	 *            参数集
	 * @return []
	 * 
	 * @return Map [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public static String parse56Url(String url) {
		// Map resultMap = new HashMap();
		String resultStr = "";
		String contextStr = HttpClientUtil.getPostResponseWithHttpClient(url,
				"utf-8");
		StringBuffer sb = new StringBuffer("");
		String opStr = "";
		if (contextStr != null && contextStr.length() > 0
				&& !"error".equals(contextStr)) {
			String titleStr = "";
			titleStr = contextStr.substring(contextStr.indexOf("<title>") + 7,
					contextStr.indexOf("</title>"));
			// resultMap.put("videoTitle", titleStr);
			String imgStr = "";
			// imgStr =
			// contextStr.substring(contextStr.indexOf("id=\"download\""),contextStr.length());
			// imgStr = imgStr.substring(imgStr.indexOf("http") + 4);
			// imgStr =
			// imgStr.substring(imgStr.indexOf("http"),imgStr.indexOf("|\""));
			// resultMap.put("videoPhoto", imgStr);
			// System.out.print(imgStr);
			String playUrlStr = "http://fcs";
			opStr = contextStr.substring(contextStr.indexOf("host=c") + 6,
					contextStr.length());
			opStr = opStr.substring(0, opStr.indexOf("."));
			playUrlStr = playUrlStr + opStr + ".56.com/flvdownload/";
			opStr = contextStr.substring(contextStr.indexOf("pURL=") + 5,
					contextStr.length());
			opStr = opStr.substring(0, opStr.indexOf("&"));
			playUrlStr = playUrlStr + opStr + "/";
			opStr = contextStr.substring(contextStr.indexOf("sURL=") + 5,
					contextStr.length());
			opStr = opStr.substring(0, opStr.indexOf("&"));
			playUrlStr = playUrlStr + opStr + "/";
			opStr = contextStr.substring(contextStr.indexOf("URLid=") + 6,
					contextStr.length());
			opStr = opStr.substring(0, opStr.indexOf("&"));
			playUrlStr = playUrlStr + opStr + ".flv?m=s";

			System.out.print(playUrlStr);
			// resultMap.put("videoUrl", playUrlStr);
			sb.append("<videoinfo TotalRecords='1'>");
			sb.append("<Row>");
			sb.append("<videoTitle>");// 视频标题
			sb.append("<![CDATA[" + titleStr + "]]>");
			sb.append("</videoTitle>");
			sb.append("<videoUrl>");// 视频url
			sb.append("<![CDATA[" + playUrlStr + "]]>");
			sb.append("</videoUrl>");
			sb.append("<videoPhoto>");// 视频封面
			sb.append("<![CDATA[" + imgStr + "]]>");
			sb.append("</videoPhoto>");
			// sb.append("<opFlag>");//操作标志
			// sb.append("<![CDATA[0]]>");
			// sb.append("</opFlag>");
			sb.append("</Row>");
			sb.append("</videoinfo>");
			resultStr = sb.toString();
		} else {
			resultStr = getOldUrl(url);
		}
		return resultStr;
	}
	
	/**
	 * <获取原视频地址>
	 * 
	 * @param sqlDataSource
	 *            参数集
	 * @return []
	 * 
	 * @return Map [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	private static String getOldUrl(String url) {
		StringBuffer sb = new StringBuffer("");
		sb.append("<videoinfo TotalRecords='1'>");
		sb.append("<Row>");
		sb.append("<videoUrl>");// 视频url
		sb.append("<![CDATA[" + url + "]]>");
		sb.append("</videoUrl>");
		sb.append("</Row>");
		sb.append("</videoinfo>");
		return sb.toString();
	}

	public String getSwfUrl() {
		return swfUrl;
	}

	public void setSwfUrl(String swfUrl) {
		this.swfUrl = swfUrl;
	}

	

	public static void main(String[] args) 
	{   
		HttpClient client = new HttpClient();
		String resultStr = "";
//		String videoUrl = "http://v.youku.com/v_playlist/f5618298o1p0.html";
		String videoUrl = "http://www.56.com/p21/v_MTAwMjM5NTc4.html";
		if (videoUrl.indexOf("Warning:") != -1) 
		{
			videoUrl = videoUrl.substring(7);
		}
		resultStr = client.parseUrl(videoUrl);
		if (resultStr.indexOf("Warning:") != -1) 
		{
			resultStr = resultStr.substring(8);
		}
		System.out.println("==============");
		System.out.println(resultStr);
		System.out.println("==============");
//		String swfurl = "http://player.youku.com/player.php/Type/Folder/Fid/5618298/Ob/1/Pt/0/sid/XMjUwODgzNDMy/v.swf";
//		resultStr = client.parseUrl(swfurl);
//		System.out.println("==============");
//		System.out.println(resultStr);
//		System.out.println("==============");
		
	}
}
